package heima3_2.cn.osc.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

/**
 * Created by yaozhanlie on 2017/1/13.
 */

public class CommonUtil {
    /**
     * 查找一个布局中所有的按钮，并设置点击事件
     *
     * @param parent
     */
    public static void findButtonsAndSetOnClickListener(
            View parent, View.OnClickListener listener) {

        if (parent instanceof ViewGroup) {
            ViewGroup p = (ViewGroup) parent;
            for (int i = 0; i < p.getChildCount(); i++) {
                View child = p.getChildAt(i);
                if (child instanceof Button || child instanceof ImageButton) {
                    // 给按钮设置点击事件
                    child.setOnClickListener(listener);
                } else if (child instanceof ViewGroup) {
                    findButtonsAndSetOnClickListener(child, listener);
                }
            }
        }
    }
}
